package com.yanqu.road.server.manger.activity.platform;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.platform.PlatformMailAntiActivityModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

public class PlatformMailAntiActivityMgr extends TempMgr {

    static ActivityInfo activityInfo;

    static Map<String, ActivityConfig> activityConfigMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload PlatformMailAnti Activity start");
        ActivityInfo tempActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PlatformActivity.getValue(), 62);
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no PlatformMailAnti Activity in show time");
            activityInfo = null;
            activityConfigMap = null;
        } else {

            tempActivityInfo = openActivityInfoList.get(0);
            int tmpActivityId = tempActivityInfo.getActivityId();

            activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
            activityInfo = tempActivityInfo;
        }

        getLogger().info("reload PlatformMailAnti Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            PlatformMailAntiActivityModule module = gamePlayer.getModule(PlatformMailAntiActivityModule.class);
            module.initUserData();
            module.syncUserData();
        }
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void setActivityInfo(ActivityInfo activityInfo) {
        PlatformMailAntiActivityMgr.activityInfo = activityInfo;
    }

    public static ActivityConfig getActivityConfig(String key){
        if(activityConfigMap == null){
            return null;
        }
        return activityConfigMap.get(key);
    }

    public static void sendMail(String userId) {
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        if(serverInfo == null){
            return;
        }
        if(!eChannelType.isMainLandChannel(serverInfo.getChannelId()) && eChannelType.MainLandInnerTest.getValue() != serverInfo.getChannelId()){
            return;
        }
        if(activityInfo == null || activityConfigMap == null){
            return;
        }
        if(!ActivityMgr.activityInTime(activityInfo)){
            return;
        }
        UserInfo userInfo = UserMgr.getUserInfo(Long.valueOf(userId));
        if(userInfo == null){
            return;
        }
        String title = activityConfigMap.get(eActivityConfigType.ANTI_FRAUD_MAIL_TITLE.getName()).getValue();
        String content = activityConfigMap.get(eActivityConfigType.ANTI_FRAUD_MAIL_CONTENT.getName()).getValue();
        String reward = activityConfigMap.get(eActivityConfigType.ANTI_FRAUD_MAIL_REWORD.getName()).getValue();

        MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), reward, ServerLanguageMgr.getContent(content, userInfo.getLanguage()),
                ServerLanguageMgr.getContent(title, userInfo.getLanguage()));
    }
}
